package webServiceClient.academico;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class FrmCadTurma extends Activity {
	private Button btnCad;
	private Button btnClose;
	private EditText edtCodTurma;
	private EditText edtNomTurma;
	private EditText edtAnoTurma;
	private Spinner cbCurso;

	private void carregaBtn() {
		btnCad = (Button) findViewById(R.frmCadTurma.btnCad);
		btnClose = (Button) findViewById(R.frmCadTurma.btnFechar);
		edtCodTurma = (EditText) findViewById(R.frmCadTurma.edtCodTurma);
		edtNomTurma = (EditText) findViewById(R.frmCadTurma.edtNomTurma);
		edtAnoTurma = (EditText) findViewById(R.frmCadTurma.edtAnoTurma);
		cbCurso = (Spinner) findViewById(R.frmCadTurma.cbCurso);

		String[] codCurso;
		try {
			WebServiceClient ws = new WebServiceClient();
			codCurso = ws.getCursos("getCursos", Definicoes.getRaAdm(),
					Definicoes.getSenhaAdm(), 1);
			ArrayAdapter<String> adp = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, codCurso);
			adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			cbCurso.setAdapter(adp);

		} catch (Exception e) {
			Toast t = Toast.makeText(this, "Erro na lista - " + e.getMessage(),
					Toast.LENGTH_LONG);
			t.show();
			return;
		}

		btnCad.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					WebServiceClient ws = new WebServiceClient();
					String s = ws.addTurma("addTurma", Definicoes.getRaAdm(),
							Definicoes.getSenhaAdm(), edtCodTurma.getText()
									.toString(), edtNomTurma.getText()
									.toString(), Integer.parseInt(edtAnoTurma
									.getText().toString()), Definicoes
									.getSessao(cbCurso.getSelectedItem()
											.toString(), "|", 1));
					if (s.equals("true")) {
						Toast t = Toast.makeText(FrmCadTurma.this,
								"Turma cadastrada com sucesso", Toast.LENGTH_LONG);
						t.show();
					} else {
						Toast t = Toast.makeText(FrmCadTurma.this,
								"Erro ao cadastrar turma - " + s,
								Toast.LENGTH_LONG);
						t.show();
					}
				} catch (Exception e) {
					Toast t = Toast.makeText(FrmCadTurma.this,
							Definicoes._erroServer + " " + e.getMessage(),
							Toast.LENGTH_LONG);
					t.show();
				}
			}
		});
		btnClose.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.frm_cad_turma);
		carregaBtn();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.frm_cad_turma, menu);
		return true;
	}
}
